package com.example.task1.ui.AllService.parking;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.task1.R;
import com.example.task1.application.MyApplication;
import com.example.task1.base.BaseActivity;
import com.example.task1.bean.ParkingPriceBean;
import com.example.task1.http.Api;
import com.example.task1.util.ApiResp;
import com.example.task1.util.CallBack;

import java.util.HashMap;

import retrofit2.Call;

public class ParkingPlayActivity extends BaseActivity {

    private RecyclerView rvList;
    private Button btnPlay;
    private ParkingPriceAdapter parkingPriceAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_parking_play);
        initView();
        initData();
        parkingPriceAdapter = new ParkingPriceAdapter(this);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        rvList.setAdapter(parkingPriceAdapter);
        rvList.setLayoutManager(linearLayoutManager);
        btnPlay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(ParkingPlayActivity.this);
                builder.setTitle("充值");
                EditText editText = new EditText(ParkingPlayActivity.this);
                editText.setHint("输入充值金额");
                builder.setView(editText);
                builder.setNegativeButton("支付", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String money = editText.getText().toString();
                        if (TextUtils.isEmpty(money)) {
                            MyApplication.show("金额不能为空");
                        } else {
                            Call<ApiResp> post = Api.api().post(MyApplication.TOKEN, "/prod-api/api/park/recharge/pay?money=" + money + "&payType=支付宝", Api.getBody(new HashMap<>()));
                            Api.send(post, ApiResp.class, new CallBack<ApiResp>() {
                                @Override
                                public void success(ApiResp data) {
                                    MyApplication.show("充值成功");
                                    finish();
                                }

                                @Override
                                public void error(String msg) {

                                }
                            });
                        }
                    }
                });
                builder.setPositiveButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });

                builder.show();
            }
        });
    }

    private void initData() {
        Call<ApiResp> call = Api.api().get(MyApplication.TOKEN, "/prod-api/api/park/recharge/list");
        Api.send(call, ParkingPriceBean.class, new CallBack<ParkingPriceBean>() {
            @Override
            public void success(ParkingPriceBean data) {
                parkingPriceAdapter.setData(data.getRows());
            }

            @Override
            public void error(String msg) {

            }
        });
    }

    @Override
    public String getMyTitle() {
        return "充值";
    }

    private void initView() {
        rvList = findViewById(R.id.rv_list);
        btnPlay = findViewById(R.id.btn_play);
    }
}